// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var _css = require("./loansMoneyList.css");
	var router = require("../hash/pageRouter");
	var tools = require("tools");
	var databind = require("databind");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		
		loadLoansMoneyList();

		initEvent();
	};
	var initEvent = function() {
		$("#loansMoneyList .blue_search_btn").unbind("click").click(function(){
			loadLoansMoneyList();
		});
		
		$(".createLonasMoney").unbind("click").click(function(){
			router.route("loansMoneyManage/loansMoneyCreate.html");
		});
	};
	
	var loadLoansMoneyList = function() {
		//搜索条件初始化
		var oData = {
			lendUnitName: $("#loansMoneyList input[name=lendUnitName]").val(),
			startDate: $("#loansMoneyList input[name=startDate]").val(),
			endDate: $("#loansMoneyList input[name=endDate]").val(),
			pageIndex: pageIndex,
			pageSize: pageSize
		};
		tools.loadList("inquireLonsMoneyInfoList.json", $("#loansMoneyList .table"), $("#loansMoneyTpl"), oData, function(){
			//详情
			$("#loansMoneyList .detail").unbind("click").click(function(){
				router.route("loansMoneyManage/loansMoneyCreate.html?loansMoneyId=" + $(this).parents("tr").attr("loansMoneyId"));
			});
			
			//删除
			$("#loansMoneyList .delete").unbind("click").click(function(){
				var $this = $(this);
				dialog.showConfirmDialog("融资删除", "是否确认删除该条融资？", function() {
					deletLonsMoneyInfoDetail($this.parents("tr").attr("loansmoneyId"));
				});
			});
			
			//确认 
			$("#loansMoneyList .confirm").unbind("click").click(function(){
				var $this = $(this);
				dialog.showConfirmDialog("融资确认", "是否确认该条融资？", function() {
					confirmLonsMoneyInfoDetail($this.parents("tr").attr("loansmoneyId"));
				});
			});
		});
	}
	
	// 删除融资
	var deletLonsMoneyInfoDetail = function(loansMoneyId) {
		base.fnAjax({
			action: "deletLonsMoneyInfoDetail.json", //接口名称
			oData: {
				loansMoneyId: loansMoneyId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.alert(1, "删除成功！");
					router.route("loansMoneyManage/loansMoneyList.html");
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	// 确认融资
	var confirmLonsMoneyInfoDetail = function(loansMoneyId) {
		base.fnAjax({
			action: "confirmLonsMoneyInfoDetail.json", //接口名称
			oData: {
				loansMoneyId: loansMoneyId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.alert(1, "确认成功！");
					router.route("loansMoneyManage/loansMoneyList.html");
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	return {
		init: init
	};
});